//
//  WPGoldCoinVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/4.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPGoldCoinVC.h"
#import "WPInputGoldCountVC.h"
#import "WPCoinTopUp.h"
#import "WPSoftwareAgreementVC.h"
#import "WPMineLoginVC.h"

@interface WPGoldCoinVC ()

@property (weak, nonatomic) IBOutlet UIImageView *bg_goldImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgImageViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cointopConstraint;
@property (weak, nonatomic) IBOutlet UILabel *coinNumLabel;

@end

@implementation WPGoldCoinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    
    self.bgImageViewHeight.constant = SCREEN_WIDTH*200/375.0;
    self.backTopConstraint.constant = 33*KWIDTH_SCALE;
    self.cointopConstraint.constant = 126*KWIDTH_SCALE;
}

-(void)loadUserCoinInfomation
{
    [self showWithStatus:nil];
    NSString *url = @"user/findUser.do";
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self dismiss];
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            
            NSDictionary *detail = responseObject[@"detail"];
            self.coinNumLabel.text = [NSString stringWithFormat:@"%@",detail[@"gold"]];
        }
        else
        {
            self.coinNumLabel.text = @"0";
        
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
             {
                 //账号过期
                 [self illegalLoginWithSuccessBlock:^{
                     
                     WPMineLoginVC *vc = [WPMineLoginVC new];
                     [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
                 }];
              }
            else
              {
                    [self showErrorInfoWithStatus:@"获取失败"];
              }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self dismiss];
        [self showErrorInfoWithStatus:@"网络错误"];
    }];

}


//重新登陆的代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        if (buttonIndex == 0)
        {
            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
            vc.successLoginBlock = ^
            {
                [self loadUserCoinInfomation];
            };
        }
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    [self loadUserCoinInfomation];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.isDefaultStyle)
    {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
    
}

- (IBAction)coinWithdrawalBtnDidClick:(UIButton *)sender
{
    if (!([self.coinNumLabel.text intValue] >= 500))
    {
        [self showErrorInfoWithStatus:@"提现医票不足"];
        return ;
    }
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPInputGoldCount" bundle:nil];
    WPInputGoldCountVC *vc = [sb instantiateInitialViewController];
    vc.coinCount = [self.coinNumLabel.text intValue];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)coinRecodeBtn:(UIButton *)sender
{
    
}

- (IBAction)backBtnDIdClick:(UIButton *)sender
{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)coinIntroduceBtnDidClick:(UIButton *)sender
{
    WPSoftwareAgreementVC *vc = [WPSoftwareAgreementVC new];
    vc.PageTitle = @"医票介绍";
    vc.url = @"https://www.wobingwoyi.com:8443/wbwy/page/ypsm.do";
    [self.navigationController pushViewController:vc animated:YES];
}

@end
